// pages/resultype/corneal/corneal.js
const commonutil = require("../commonutil.js");
const screenresultservice = require("../../../apiservice/screen/screenresultservice.js");
const jsonData = require("../jsondata.js");
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		scid: '',
		customerid: '',
		leadName: '',
		addOrMod: 'add',
		//角膜检查选项集
		result: [],
		conjunctivaIndex: -1,
		pterygiumIndex: -1,
		cornealNeoplasiaIndex: -1,
		pterygiumFirstDiagnosisIndex: -1,
		xerophthalmiaIndex: -1,
		cornealNeoplasiaFirstDiagnosisIndex: -1,
		normalOrAbnormal: jsonData.normalOrAbnormal,
		noneOrBothOrLeftOrRight: jsonData.noneOrBothOrLeftOrRight,
		pterygiumArray: jsonData.pterygiumArray,
    schoolId:'',

		/**角膜检查 */
		//结膜
		conjunctiva: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0017',
			new_contentid: 'NR0066',
			new_result: 'SC0000000013',
			new_doctorid: null,
			ownerid: null
		},
		//结膜异常描述
		conjunctivaDescription: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0017',
			new_contentid: 'NR0068',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//翼状胬肉
		pterygium: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0023',
			new_contentid: 'NR0069',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//翼状胬肉描述
		pterygiumDescription: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0023',
			new_contentid: 'NR0071',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//角膜新生物
		cornealNeoplasia: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0006',
			new_contentid: 'NR0007',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//角膜新生物描述
		cornealNeoplasiaDescription: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0006',
			new_contentid: 'NR0073',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//角膜
		corneal: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0005',
			new_contentid: 'NR0074',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//泪膜破碎时间
		tearFilmBrokenTime: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0019',
			new_contentid: 'NR0077',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//翼状胬肉-初步诊断
		pterygiumFirstDiagnosis: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0003',
			new_contentid: 'NR0078',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//干眼评分表
		xerophthalmiaScale: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0003',
			new_contentid: 'NR0087',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//干眼症
		xerophthalmia: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0003',
			new_contentid: 'NR0085',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//角膜新生物-初步诊断
		cornealNeoplasiaFirstDiagnosis: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0003',
			new_contentid: 'NR0088',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//其他角膜病
		otherCornealDiseases: {
			new_scid: null,
			new_categoryid: 'LB0002',
			new_itemid: 'XM0003',
			new_contentid: 'NR0091',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var options = options;
		if (options) {
			this.setData({
				leadName: options.leadname,
				customerid: options.sccustid,
				scid: options.scid,
				categoryid: options.categoryid
			})
			this.getCornealResults(options.scid, options.sccustid, options.categoryid);
      if(options.schoolId){
        this.setData({schoolId:options.schoolId});
      }
		}
	},
	//角膜检查提交
	cornealSubmit: function() {
		var createResult = this.getCorneal();
		var that = this;
		commonutil.createScreenResults(createResult, function(res) {
			var idArray = res.result.split('|');
			for(var i = 0;i < idArray.length;i++){
				createResult[i].new_sc_resultId = idArray[i];
			}
			that.setData({
				addOrMod: "mod",
				conjunctiva:	createResult[0],
				conjunctivaDescription:	createResult[1],
				pterygium:	createResult[2],
				pterygiumDescription:	createResult[3],
				cornealNeoplasia:	createResult[4],
				cornealNeoplasiaDescription:	createResult[5],
				corneal:	createResult[6],
				tearFilmBrokenTime:	createResult[7],
				pterygiumFirstDiagnosis:	createResult[8],
				xerophthalmiaScale:	createResult[9],
				xerophthalmia:	createResult[10],
				cornealNeoplasiaFirstDiagnosis:	createResult[11],
				otherCornealDiseases:	createResult[12],
			});
		});
	},
	//角膜检查更新
	cornealUpdate:function(){
		var updateResult = this.getCorneal();
		commonutil.updateScreenResults(updateResult);
	},
	//查看报告
	enterDetail: async function(e) {
		var scid = this.data.scid;
		var leadid = this.data.customerid;
		var lead = {};
		var data = await commonutil.getLeadInfo(leadid);
		lead.leadid = data.leadId;
		lead.customername = data.lastName;
		lead.age = data.new_age;
		lead.sex = data.new_gender;
		lead.iphoneno1 = data.new_phone;
		lead.sfz = data.new_citizenid;
		lead.createdon = data.modifiedOn;
    var url = "/pages/print/letter/letter?scid=" + scid;
    if (this.data.schoolId)
      url += "&schoolId=" + this.data.schoolId;
		wx.navigateTo({
			url: url,
			events: {
				// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				acceptDataFromOpenedPage: function(data) {
					console.log(data)
				},
				someEvent: function(data) {
					console.log(data)
				}
			},
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: lead
				})
			}
		})
	},
	//获取结果集
	getCornealResults: async function(scid, customerid, categoryid) {
		wx.showLoading({
			title:'加载中...',
			mask:true,
		})
		var success = await screenresultservice.GetScreenResultByCategory(scid, customerid, categoryid);
		var reusltArray = success.data
		if(reusltArray.length==0){
			wx.hideLoading();
			return ;
		}
		this.setData({
			addOrMod:'mod'
		})
		for (let index in reusltArray) {
			var element = reusltArray[index];
			if (element.new_contentid === 'NR0066') {
				this.setData({
					conjunctiva: element
				})
			} else if (element.new_contentid === 'NR0068') {
				this.setData({
					conjunctivaDescription: element
				})
			} else if (element.new_contentid === 'NR0069') {
				this.setData({
					pterygium: element
				})
			} else if (element.new_contentid === 'NR0071') {
				this.setData({
					pterygiumDescription: element
				})
			} else if (element.new_contentid === 'NR0007') {
				this.setData({
					cornealNeoplasia: element
				})
			}else if (element.new_contentid === 'NR0073') {
				this.setData({
					cornealNeoplasiaDescription: element
				})
			} else if (element.new_contentid === 'NR0074') {
				var optionsArray = element.new_result.split(',');
				this.setData({
					corneal: element,
					result:optionsArray,
				})
			} else if (element.new_contentid === 'NR0077') {
				this.setData({
					tearFilmBrokenTime: element
				})
			} else if (element.new_contentid === 'NR0078') {
				this.setData({
					pterygiumFirstDiagnosis: element
				})
			}else if (element.new_contentid === 'NR0087') {
				this.setData({
					xerophthalmiaScale: element
				})
			} else if (element.new_contentid === 'NR0085') {
				this.setData({
					xerophthalmia: element
				})
			} else if (element.new_contentid === 'NR0088') {
				this.setData({
					cornealNeoplasiaFirstDiagnosis: element
				})
			} else if (element.new_contentid === 'NR0091') {
				this.setData({
					otherCornealDiseases: element
				})
			}
		}
		wx.hideLoading();
	},
	/**角膜检查选项集、文本赋值 */
	onConjunctivaChange: function(e) {
		var result = 'conjunctiva.new_result';
		this.setData({
			[result]:'',
			conjunctivaIndex: e.detail.index
		})
	},
	onPterygiumChange: function(e) {
		var result = 'pterygium.new_result';
		this.setData({
			[result]:'SC0000000029',
			pterygiumIndex: e.detail.index
		})
	},
	onCornealNeoplasiaChange: function(e) {
		var result = 'cornealNeoplasia.new_result';
		this.setData({
			[result]:'SC0000000029',
			cornealNeoplasiaIndex: e.detail.index
		})
	},
	onCornealChange: function(e) {
		this.setData({
			result: e.detail
		})
	},
	onPterygiumFirstDiagnosisChange: function(e) {
		this.setData({
			pterygiumFirstDiagnosisIndex: e.detail.index
		})
	},
	onXerophthalmiaChange: function(e) {
		this.setData({
			xerophthalmiaIndex: e.detail.index
		})
	},
	onCornealNeoplasiaFirstDiagnosisChange: function(e) {
		this.setData({
			cornealNeoplasiaFirstDiagnosisIndex: e.detail.index
		})
	},
	onOtherCornealDiseasesChange: function(e) {
		var otherCornealDiseasesResult = 'otherCornealDiseases.new_result';
		this.setData({
			[otherCornealDiseasesResult]: e.detail
		})
	},
	onXerophthalmiaScaleChange: function(e) {
		var xerophthalmiaScaleResult = 'xerophthalmiaScale.new_result';
		this.setData({
			[xerophthalmiaScaleResult]: e.detail
		})
	},
	onTearFilmBrokenTimeChange: function(e) {
		var tearFilmBrokenTimeResult = 'tearFilmBrokenTime.new_result';
		this.setData({
			[tearFilmBrokenTimeResult]: e.detail
		})
	},
	onCornealNeoplasiaDescriptionChange: function(e) {
		var cornealNeoplasiaDescriptionResult = 'cornealNeoplasiaDescription.new_result';
		this.setData({
			[cornealNeoplasiaDescriptionResult]: e.detail
		})
	},
	onPterygiumDescriptionChange: function(e) {
		var pterygiumDescriptionResult = 'pterygiumDescription.new_result';
		this.setData({
			[pterygiumDescriptionResult]: e.detail
		})
	},
	onConjunctivaDescriptionChange: function(e) {
		var conjunctivaDescriptionResult = 'conjunctivaDescription.new_result';
		this.setData({
			[conjunctivaDescriptionResult]: e.detail
		})
	},
	//角膜检查
	getCorneal: function() {
		var createResult = [];
		var scid = this.data.scid;
		var customerid = this.data.customerid;
		var conjunctiva = this.data.conjunctiva;
		var conjunctivaDescription = this.data.conjunctivaDescription;
		var pterygium = this.data.pterygium;
		var pterygiumDescription = this.data.pterygiumDescription;
		var cornealNeoplasia = this.data.cornealNeoplasia;
		var cornealNeoplasiaDescription = this.data.cornealNeoplasiaDescription;
		var corneal = this.data.corneal;
		var tearFilmBrokenTime = this.data.tearFilmBrokenTime;
		var pterygiumFirstDiagnosis = this.data.pterygiumFirstDiagnosis;
		var xerophthalmiaScale = this.data.xerophthalmiaScale;
		var xerophthalmia = this.data.xerophthalmia;
		var cornealNeoplasiaFirstDiagnosis = this.data.cornealNeoplasiaFirstDiagnosis;
		var otherCornealDiseases = this.data.otherCornealDiseases;

		//结膜
		var conjunctivaIndex = this.data.conjunctivaIndex;
		var normalOrAbnormal = this.data.normalOrAbnormal;
		if (conjunctivaIndex >-1) {
			conjunctiva.new_result = normalOrAbnormal[conjunctivaIndex];
		} 
		//翼状胬肉
		var pterygiumIndex = this.data.pterygiumIndex;
		var noneOrBothOrLeftOrRight = this.data.noneOrBothOrLeftOrRight;
		
		if (pterygiumIndex >-1) {
			pterygium.new_result = noneOrBothOrLeftOrRight[pterygiumIndex];
		} 
		//角膜新生物
		var cornealNeoplasiaIndex = this.data.cornealNeoplasiaIndex;
		if (cornealNeoplasiaIndex>-1) {
			cornealNeoplasia.new_result =  noneOrBothOrLeftOrRight[cornealNeoplasiaIndex];
		}
		//角膜
		var selected = this.data.result;
		var str = '';
		if (selected != null && selected.length > 0) {
			str = selected.join(",");
		}
		corneal.new_result = str;
		//翼状胬肉-初步诊断
		var pterygiumFirstDiagnosisIndex = this.data.pterygiumFirstDiagnosisIndex;
		var pterygiumArray = this.data.pterygiumArray;
		if (pterygiumFirstDiagnosisIndex>-1) {
			pterygiumFirstDiagnosis.new_result = pterygiumArray[pterygiumFirstDiagnosisIndex];
		} 
		//干眼症
		var xerophthalmiaIndex = this.data.xerophthalmiaIndex;
		if (xerophthalmiaIndex >-1) {
			xerophthalmia.new_result =  noneOrBothOrLeftOrRight[xerophthalmiaIndex];
		} 
		// 角膜新生物-初步诊断
		var cornealNeoplasiaFirstDiagnosisIndex = this.data.cornealNeoplasiaFirstDiagnosisIndex;
		if (cornealNeoplasiaFirstDiagnosisIndex >-1) {
			cornealNeoplasiaFirstDiagnosis.new_result = noneOrBothOrLeftOrRight[cornealNeoplasiaFirstDiagnosisIndex];
		} 
		createResult.push(conjunctiva);
		if(conjunctiva.new_result=='SC0000000005'&&conjunctivaDescription.new_result!=''){
			createResult.push(conjunctivaDescription);
		}		
		createResult.push(pterygium);
		if(pterygium.new_result!='SC0000000029'&&pterygiumDescription.new_result!=''){
			createResult.push(pterygiumDescription);
		}
		createResult.push(cornealNeoplasia);
		if(cornealNeoplasia.new_result!='SC0000000029'&&cornealNeoplasiaDescription.new_result!=''){
			createResult.push(cornealNeoplasiaDescription);
		}
		if(str!=''){
			createResult.push(corneal);
		}	
		if(tearFilmBrokenTime.new_result!=''){
			createResult.push(tearFilmBrokenTime);
		}	
		createResult.push(pterygiumFirstDiagnosis);
		if(xerophthalmiaScale.new_result!=''){
			createResult.push(xerophthalmiaScale);
		}
		createResult.push(xerophthalmia);
		createResult.push(cornealNeoplasiaFirstDiagnosis);
		if(otherCornealDiseases.new_result!=''){
			createResult.push(otherCornealDiseases);
		}
		//给各结果的筛查id/联系人id赋值
		createResult.forEach(element => {
			element.new_scid = scid;
			element.new_customerid = customerid;
		});
		return createResult;
	}

})
